package pro.lingwu.station.service.impl;

import org.springframework.stereotype.Service;
import pro.lingwu.station.exception.HasValueException;
import pro.lingwu.station.dao.TicketMapper;
import pro.lingwu.station.dto.db.Ticket;
import pro.lingwu.station.service.TicketService;

import java.util.List;

/**
 * @author @lingwu
 * @date created in 12/27/2021
 */
@Service
public class TicketServiceImpl implements TicketService {
    private final TicketMapper mapper;

    public TicketServiceImpl(TicketMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public void buyTicket(Ticket ticket) throws HasValueException {
        ticket.setSeat(null);
        ticket.setStatus(1);
        Ticket search = new Ticket();
        search.setNoId(ticket.getNoId());
        search.setPassenger(ticket.getPassenger());
        var res = mapper.findTicketBy(search);
        if (res.size()>0 && res.get(0).getStatus().equals(1)) {
            throw new HasValueException();
        }
        mapper.updateTicket(ticket);
    }

    @Override
    public void rmTicket(Ticket ticket) {
        ticket.setSeat(null);
        ticket.setStatus(2);
        mapper.updateTicket(ticket);
    }

    @Override
    public List<Ticket> findTickets(Ticket ticket) {
        return mapper.findTicketBy(ticket);
    }
}
